import Axios from "axios";

import * as pubApi from '@api/public'

import * as API from "@api/common/common";
import { Store } from "vuex";

export const state = {
    userId:localStorage.getItem('userId')?localStorage.getItem('userId'):"",
    userName:localStorage.getItem('userName')?localStorage.getItem('userName'):"",
    isLogin:0,
    bacStore:"",  //背景颜色
    dialogshow: false,  //弹窗展示
    uKey:pubApi.getCookie("uKey")==""?'':pubApi.getCookie("uKey"),
    uTicket: pubApi.getCookie("uTicket")=="" ?'':pubApi.getCookie("uTicket")
}

export const mutations = {
    setdialogshow: (state) => {
        state.dialogshow = true
    },
    bacStore: (state,val) => {
        state.bacStore = val
    },
    changeLogin (state, user) {   //登录成功设置token
        state.userName=user.name
        state.userId=user.idstr
        localStorage.setItem('userId',user.idstr)
        localStorage.setItem('userName',user.name)
        state.isLogin=1
      }
}


export const actions = {
    setdialogshow: ({ commit }) => {
        commit('setdialogshow')
    },
    bacStore: ({ commit },params) => {
        commit('bacStore',params)
    },
    changeLogin: ({ commit },params) => {
// 返回一个promise对象
       return new Promise((resolve, reject) => {
        API.post("/v1/flash/login",params)
                 .then((res) => {
                   if(res.IsPositive){
                    commit("changeLogin",res.Data)
                   }
                   resolve(res);
                 },(error)=>{
                    reject(error)
                 })
           });
       }
}

export default { state, mutations, actions }